<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['merch_types']=$this->merch_types;
$return['merch_types2']=$this->merch_types2;
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_category',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['url']='jy_merch/index//cid='.$v['id'];
		$v['allurl']=custom_url($v['url'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['isrecommand']=array('0'=>'不推荐','1'=>'推荐');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'分类管理'),
			array('op'=>'edit','title'=>'添加分类'),
			array('op'=>'setting','title'=>'类型自定义'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：
		<red>
		</br>1,同一客户，可以跟不同类型的商户绑定推广关系；同一类型不能重复；
		</br>2,每个商户有自己的会员体系（具体如何成为商户的会员，请移步基本设置--商户会员体系）；
		</red>
		','list'=>array(			
			array('name'=>'catename','title'=>'标题','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'merch_types2'),
			array('name'=>'pcate','title'=>'商品分类','type'=>'select','options'=>'pcates'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'allurl','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'短地址','type'=>'copy'),
			array('name'=>'allurl','title'=>'长地址','type'=>'copy'),
			array('name'=>'status','title'=>'是否显示','type'=>'radio','options'=>'status'),	
				
		)),
	);
	$return['htmls']=$htmls;
	$return['pcates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'mid'=>0,'parentid'=>0),array('id','title'),'id');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_merch_category',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'不显示','1'=>'显示');
	$return['isrecommand']=array('0'=>'不推荐','1'=>'推荐');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'分类设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'分类管理'),
			array('op'=>'edit','title'=>'添加分类')
		),
		'edit'=>array(
			array('name'=>'displayorder','title'=>'排序','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'merch_types2'),
			array('name'=>'pcate','title'=>'商品分类','type'=>'select','options'=>'pcates','help'=>'<red>商品分类，是满足一些商户，绑定指定的平台商品分类，这样商户可以抢单指定该分类下面的订单（部分插件会用到）！</red>'),
			array('name'=>'catename','title'=>'标题','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'status','title'=>'是否显示','type'=>'radio','options'=>'status'),	
			array('name'=>'isrecommand','title'=>'是否推荐','type'=>'radio','options'=>'isrecommand'),			
			)
	);
	$return['pcates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>0,'mid'=>0,'parentid'=>0,'onepluginname'=>''),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['catename'] ){
		exi('请填写标题','error');
	}
	$data=array_fields(array('pcate','catename','displayorder','image','isrecommand','status','type'),$postdata);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_merch_category',$data,array('id'=>$id));
		exi('设置成功！','','/cate/');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=time();
		pdo_insert('jy_merch_category',$data);
		exi('添加成功！','','/cate/');
	}
}
if($_RQ['op']=='setting'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->plugin_setting('setting',$postdata);
		exi('设置成功');
	}
	$merch_types=array(
		array('id'=>'1','title'=>'餐饮','nodel'=>1),
		array('id'=>'2','title'=>'美业','nodel'=>1),
		array('id'=>'3','title'=>'娱乐','nodel'=>1),
		array('id'=>'4','title'=>'周边游','nodel'=>1),
		array('id'=>'5','title'=>'服装','nodel'=>1),
		array('id'=>'6','title'=>'购票','nodel'=>1),
		array('id'=>'7','title'=>'汽修','nodel'=>1),
		array('id'=>'8','title'=>'鲜花蛋糕','nodel'=>1),
		array('id'=>'9','title'=>'母婴童装','nodel'=>1),
		array('id'=>'10','title'=>'生鲜','nodel'=>1),
		array('id'=>'11','title'=>'服装服饰','nodel'=>1),
		array('id'=>'12','title'=>'相亲交友','nodel'=>1),
		array('id'=>'13','title'=>'人才中介','nodel'=>1),
	);
	$return['item']=$this->plugin_setting('setting');
	$return['item']['merch_types']=$return['item']['merch_types']?:$merch_types;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'类型设置',
		'edit'=>array(
			array('title'=>'[温馨提示]：涉及默认展示，默认类型，不支持删除。','type'=>'tip'),
			array(
				'type'=>'array',
				'name'=>'merch_types',
				'nosplice'=>1,
				'title'=>'类型',
				'add_tip'=>'添加一个类型',
					'list'=>array(
						array('type'=>'string','name'=>'id','title'=>'ID'),
						array('type'=>'string','name'=>'title','title'=>'名称'),
					),
					'details'=>array(
						array('type'=>'copy','name'=>'id','title'=>'ID'),
						array('type'=>'string','name'=>'title','title'=>'名称'),
					)
				),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_merch_category',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_merch_category',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>